<template>
  <div class="detail container">
    <header>
      <div class="header-returns" v-show="isShow">
        <div @click="goBack"><i class="iconfont icon-fanhui"></i></div>
        <div><i class="iconfont icon-home"></i></div>
      </div>
      <div class="header-bar" v-show="!isShow" :style="styleOption">
        <div @click="goBack"><i class="iconfont icon-fanhui"></i></div>
        <div>
          <span>商品详情</span>
          <span>商品评价</span>
        </div>
        <div><i class="iconfont icon-home"></i></div>
      </div>
    </header>
    <section ref="wrapper">
      <div>
        <div class="swiper-main">
          <swiper :options="swiperOption">
            <swiper-slide v-for="(item, index) in swiperList" :key="index">
              <img :src="item.imgUrl" alt="" />
            </swiper-slide>
          </swiper>
          <div class="swiper-pagination"></div>
        </div>
        <div class="goods-name">
          <h1>{{ goods.name }}</h1>
          <p>兰香中夹杂桂香，清醇爽口。</p>
        </div>
        <div class="goods-price">
          <div class="oprice">
            <span>￥</span>
            <b>{{ goods.price }}</b>
          </div>
          <div class="pprice">
            <span>价格：</span>
            <del>￥138</del>
          </div>
        </div>
        <div>
          <img style="width: 100%; height: 300px" :src="goods.imgUrl" alt="" />
          <img style="width: 100%; height: 300px" :src="goods.imgUrl" alt="" />
          <img style="width: 100%; height: 300px" :src="goods.imgUrl" alt="" />
        </div>
      </div>
    </section>
    <footer>
      <!-- <div class="add-cart" >加入购物车</div>
      <div>立即购买</div> -->
      <van-goods-action>
        <van-goods-action-icon icon="chat-o" text="客服" color="#ee0a24" />
        <van-goods-action-icon icon="cart-o" text="购物车" />
        <van-goods-action-icon icon="star" text="已收藏" color="#ff5000" />
        <van-goods-action-button
          type="warning"
          text="加入购物车"
          @click="addCart"
        />
        <van-goods-action-button type="danger" text="立即购买" />
      </van-goods-action>
    </footer>
  </div>
</template>

<script>
import "swiper/dist/css/swiper.css";
import { swiper, swiperSlide } from "vue-awesome-swiper";
import BetterScroll from "better-scroll";
import { Toast } from "mint-ui";
import http from "@/common/api/request";
export default {
  name: "Swiper",
  data() {
    return {
      id: 0,
      goods: [],
      styleOption: {},
      BetterScrll: "",
      isShow: true,
      swiperOption: {
        //swiper3
        autoplay: 3000,
        speed: 1000,
        pagination: {
          el: ".swiper-pagination",
          type: "fraction",
        },
      },
      swiperList: [
        {
          imgUrl: "./images/detail1.jpeg",
        },
        {
          imgUrl: "./images/like.jpeg",
        },
        {
          imgUrl: "./images/like.jpeg",
        },
      ],
    };
  },
  components: {
    swiper,
    swiperSlide,
  },
  created() {
    this.id = this.$route.query.id;
    this.getData();
  },
  mounted() {
    this.BetterScroll = new BetterScroll(this.$refs.wrapper, {
      probeType: 3,
      click: true,
      bounce: false,
    });

    this.BetterScroll.on("scroll", (pos) => {
      let scrollY = Math.abs(pos.y);
      let opacity = scrollY / 300;
      opacity = opacity > 1 ? 1 : opacity;
      if (scrollY >= 200) {
        this.isShow = false;
        this.styleOption = {
          opacity: opacity,
        };
      } else {
        this.isShow = true;
      }
    });
  },
  activated() {
    if (this.$route.query.id != this.id) {
      this.getData();
      this.id = this.$route.query.id;
    }
  },
  methods: {
    async getData() {
      let id = this.$route.query.id;
      let res = await http.$axios({ url: "/api/goods/id", params: { id } });
      this.goods = res;
    },
    //加入购物车
    addCart() {
      http
        .$axios({
          url: "/api/addCart",
          method: "post",
          data: {
            goodsId: this.$route.query.id,
          },
          headers: {
            token: true,
          },
        })
        .then((res) => {
          if (res.success) {
            Toast("添加购物车成功");
          }
        });
    },

    //返回上一页
    goBack() {
      this.$router.back();
    },
  },
};
</script>

<style lang="scss" scoped>
header {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 1.1733rem;
  z-index: 999;
  .header-returns {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 1.1733rem;
    div {
      margin: 0 0.2667rem;
      width: 0.9333rem;
      height: 0.9333rem;
      line-height: 0.9333rem;
      text-align: center;
      background-color: rgba(0, 0, 0, 0.2);
      border-radius: 50%;
      i {
        font-size: 0.64rem;
        color: #fff;
      }
    }
  }
  .header-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 1.1733rem;
    line-height: 1.1733rem;
    background-color: #fff;
    i {
      margin: 0 0.2667rem;
      font-size: 0.5333rem;
    }
    span {
      padding: 0 0.2667rem;
      font-size: 0.4267rem;
    }
  }
}
.swiper-main {
  position: relative;
  width: 100%;
  height: 10rem;
}
.swiper-container {
  width: 100%;
  height: 10rem;
}
.swiper-main img {
  width: 100%;
  height: 10rem;
}
.swiper-pagination {
  position: absolute;
  width: 0.8267rem;
  bottom: 0.2667rem;
  text-align: right;
  border-radius: 0.1067rem;
  color: #fff;
  font-size: 0.3733rem;
  background: rgb(0, 0, 0, 0.3);
}
.swiper-pagination-fraction,
.swiper-pagination-custom,
.swiper-container-horizontal > .swiper-pagination-bullets {
  left: 88%;
}
.goods-name {
  padding: 0.5333rem 0.2667rem;
  border-bottom: 1px solid #999;
  h1 {
    font-size: 0.48rem;
    font-weight: 500;
  }
  p {
    padding: 0.1333rem 0;
    font-size: 0.3733rem;
    color: #999;
  }
}
.goods-price {
  padding: 0.5333rem 0.2667rem;
  .oprice {
    color: red;
    span {
      font-size: 0.32rem;
    }
  }
  .pprice {
    color: #999;
    font-size: 0.3733rem;
  }
}
// footer {
//   display: flex;
//   justify-content: center;
//   align-items: center;
//   position: fixed;
//   bottom: 0;
//   left: 0;
//   width: 100%;
//   height: 1.2rem;
//   border-top: 1px solid #ccc;
//   div {
//     width: 50%;
//     line-height: 1.2rem;
//     text-align: center;
//     font-size: 0.4286rem;
//     color: #fff;
//     background-color: red;
//     &.add-cart {
//       background-color: #ff9500;
//     }
//   }
// }
</style>
